﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using ReceiptPrint.Models;
using WalkingTec.Mvvm.Core;

namespace ReceiptPrint
{
    public class DataContext : FrameworkContext
    {
        public DbSet<FrameworkUser> FrameworkUsers { get; set; }
        public DbSet<ReceiptInfo> ReceiptInfo { get; set; }
        public DbSet<Template> Template { get; set; }
        public DbSet<ExcelFile> ExcelFile { get; set; }
        public DbSet<CommodityType> CommodityType { get; set; }

        public DataContext(CS cs)
             : base(cs)
        {
        }

        public DataContext(string cs, DBTypeEnum dbtype)
            : base(cs, dbtype)
        {
        }

        public DataContext(string cs, DBTypeEnum dbtype, string version = null)
            : base(cs, dbtype, version)
        {
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<CommodityType>()
           .HasIndex(b => b.Name)
           .IsUnique();

        }

            public DataContext(DbContextOptions<DataContext> options) : base(options) { }

        public override async Task<bool> DataInit(object allModules, bool IsSpa)
        {
            var state = await base.DataInit(allModules, IsSpa);
            bool emptydb = false;
            try
            {
                emptydb = Set<FrameworkUser>().Count() == 0 && Set<FrameworkUserRole>().Count() == 0;
            }
            catch { }
            if (state == true || emptydb == true)
            {
                //when state is true, means it's the first time EF create database, do data init here
                //当state是true的时候，表示这是第一次创建数据库，可以在这里进行数据初始化
                var user = new FrameworkUser
                {
                    ITCode = "admin",
                    Password = Utils.GetMD5String("123456"),
                    IsValid = true,
                    Name = "Admin"
                };

                var userrole = new FrameworkUserRole
                {
                    UserCode = user.ITCode,
                    RoleCode = "001"
                };
                
                Set<FrameworkUser>().Add(user);
                Set<FrameworkUserRole>().Add(userrole);

                //var user2 = new FrameworkUser
                //{
                //    ITCode = "user",
                //    Password = Utils.GetMD5String("123456"),
                //    IsValid = true,
                //    Name = "管理员"
                //};
                //var userrole2 = new FrameworkUserRole
                //{
                //    UserCode = user2.ITCode,
                //    RoleCode = "002"
                //};




                List<CommodityType> commodityTypes = new List<CommodityType>() 
                {
                    new CommodityType{ID = Global.GetId(), Name = "食品", Tax = Tax.C},
                    new CommodityType{ID = Global.GetId(), Name = "奶粉", Tax = Tax.C},
                    new CommodityType{ID = Global.GetId(), Name = "保健品", Tax = Tax.C},
                    new CommodityType{ID = Global.GetId(), Name = "非处方药", Tax = Tax.C},

                    new CommodityType{ID = Global.GetId(), Name = "母婴用品", Tax = Tax.D},
                    new CommodityType{ID = Global.GetId(), Name = "美妆与个人护理", Tax = Tax.D},
                    new CommodityType{ID = Global.GetId(), Name = "服装", Tax = Tax.D},
                    new CommodityType{ID = Global.GetId(), Name = "鞋", Tax = Tax.D},
                    new CommodityType{ID = Global.GetId(), Name = "日用百货", Tax = Tax.D},
                    new CommodityType{ID = Global.GetId(), Name = "首饰配饰", Tax = Tax.D},
                    new CommodityType{ID = Global.GetId(), Name = "玩具文具", Tax = Tax.D},
                    new CommodityType{ID = Global.GetId(), Name = "小家电", Tax = Tax.D},
                    new CommodityType{ID = Global.GetId(), Name = "箱包", Tax = Tax.D},

                    new CommodityType{ID = Global.GetId(), Name = "葡萄酒", Tax = Tax.E},
                    new CommodityType{ID = Global.GetId(), Name = "啤酒", Tax = Tax.E},

                };
                Set<CommodityType>().AddRange(commodityTypes);
                await SaveChangesAsync();
            }
            return state;
        }

    }

    /// <summary>
    /// DesignTimeFactory for EF Migration, use your full connection string,
    /// EF will find this class and use the connection defined here to run Add-Migration and Update-Database
    /// </summary>
    public class DataContextFactory : IDesignTimeDbContextFactory<DataContext>
    {
        public DataContext CreateDbContext(string[] args)
        {
            return new DataContext("your full connection string", DBTypeEnum.SqlServer);
        }
    }

}
